92536213 การโปรแกรมคอมพ วเตอร () Lecture 09 Input/Output with files ดร. ป ต พร ถนอมงาม ดร. ก ตต พงศ อำนวยสว สด ว ทยาล ยนาโนเทคโนโลย พระจอมเกล าลาดกระบ ง สถาบ นเทคโนโลย พระจอมเกล าเจ าค ณทหารลาดกระบ ง Input/Output with files 1
Contents แฟ มข อม ล การสร างแฟ มข อม ล การนำเข าและนำออกของข อม ลจากแฟ มข อม ล Input/Output with files 2
แฟ มข อม ล แฟ มข อม ล (data file) ค อ แฟ มท ม การเก บข อม ลท ม ความส มพ นธ ก นมาไว ด วยก น โดยม การเก บข อม ลอย างต อเน องก นไปต งแต ต นแฟ มข อม ลไปจนกระท งจบแฟ มข อม ล โดยท ผ เข ยนข อม ลสามารถแบ งข อม ลท ต องการจ ดเก บลงในแฟ มเป น field หร อ record ก ได 10 records 3 fields Input/Output with files 3
การประมวลผลแฟ มข อม ล การบ นท กข อม ลลงในแฟ มข อม ล (write data into file) การอ านข อม ลจากแฟ มข อม ลมาใช งาน (read data from file) การเพ มข อม ลลงในแฟ มข อม ล (append data into file) เป ดแฟ มข อม ล fopen() + mode ประมวลผลแฟ มข อม ล ป ดแฟ มข อม ล fclose() Input/Output with files 4
การประมวลผลแฟ มข อม ล การบ นท กข อม ลลงในแฟ มข อม ล (write data into file) เป ดแฟ มข อม ล fopen() + mode = w (overwrite) ต วอ กษรต วเด ยว (single character) ใคำส ง putc( ) ประมวลผลแฟ มข อม ล เลขจ านวนเต ม (integer) จำนวนทศนย ม (floationg point) สตร ง (strings) ใช คำส ง fprintf( ) ข อม ลแบบแบบโครงสร าง (structures) หร อต วแปรช ด (arrays) ใช คำส ง fwrite( ) ป ดแฟ มข อม ล fclose() Input/Output with files 5
การประมวลผลแฟ มข อม ล การอ านข อม ลจากแฟ มข อม ลมาใช งาน (read data from file) เป ดแฟ มข อม ล fopen() + mode = r (read only) ต วอ กษรต วเด ยว (single character) ใคำส ง getc( ) ประมวลผลแฟ มข อม ล เลขจ านวนเต ม (integer) จำนวนทศนย ม (floationg point) สตร ง (strings) ใช คำส ง fscanf( ) ข อม ลแบบแบบโครงสร าง (structures) หร อต วแปรช ด (arrays) ใช คำส ง fread( ) ป ดแฟ มข อม ล fclose() Input/Output with files 6
การประมวลผลแฟ มข อม ล การเพ มข อม ลลงในแฟ มข อม ล (append data into file) เป ดแฟ มข อม ล fopen() + mode = a (append) ประมวลผลแฟ มข อม ล ป ดแฟ มข อม ล fclose() Input/Output with files 7
เป ดแฟ มข อม ล ฟ งก ช น fopen( ) เป นฟ งก ช นท ใช ในการเป ดแฟ มข อม ล FILE *fp; fp = fopen( filename,mode); #include <stdio.h> int main() FILE *fp; if((fp=fopen( D:/test.dat, w ))==NULL) printf( Error in open file ); exit(1); else printf( Open file complete ); exit(1); fclose(); Input/Output with files 8
#include <stdio.h> #include <stdlib.h> Examples int main(void) FILE *fp; char ch; if((fp=fopen("d:/name.dat","w"))==null) printf("error in open file"); exit(1); else printf("please enter your name (press Enter to quit) : "); do ch=getche(); putc(ch,fp); while(ch!='\r'); printf("\nyour name are record at D:/name.dat\n\n"); fclose(fp); Input/Output with files 9
#include <stdio.h> #include <stdlib.h> Examples int main(void) FILE *fp; char ch; if((fp=fopen("d:/name.dat","r"))==null) printf("error in open file"); exit(1); else printf("\n Hello : "); do ch=getc(fp); printf("%c",ch); while(ch!= EOF); printf("\n\n"); fclose(fp); Input/Output with files 10
#include <stdio.h> #include <stdlib.h> int main(void) int i,n,min,max,sum,a[100]; char ch,filename[100]; float avg; FILE *fp; Examples printf("enter your input file : "); gets(filename); if((fp=fopen(filename,"a+t"))==null) printf("error in open file"); exit(1); else i=0; while (!feof(fp)) fscanf(fp,"%d",&a[i]); i++; n=i; // Finding MAX/MIN min=a[0]; max=a[0]; for (i=1;i<n;i++) if (a[i]<min) min=a[i]; else if (a[i]>max) max=a[i]; fprintf(fp,"\n\nmin = %d / MAX = %d",min,max); // Average sum=0; for (i=0;i<n;i++) sum=sum+a[i]; avg=sum/n; fprintf(fp," \n\navg = %0.2f",avg); printf("\nresult is saved at '%s'\n",filename); fclose(fp); Input/Output with files 11
Homeworks กำหนดให น กศ กษาห องหน ง ม จำนวนน กศ กษาท งหมด N คน จงเข ยนโปรแกรมต ดเกรด ในกรณ ต อไปน ผ ใช สามารถกำหนดช วงคะแนนของแต ละเกรดได เอง ร บ ช อ-รห สน กศ กษา-คะแนน จากแฟ มข อม ล และแสดงเกรดบนหน าจอ คำนวนคะแนนเฉล ยของน กเร ยนห องน แสดงค าคะแนนส งส ด และ คะแนนต ำส ด ของน กเร ยนห องน แสดงตารางแสดงความถ สะสมของแต ละเกรด แสดงค าส วนเบ ยงเบนมาตรฐานของน กศ กษาห องน บ นท ก ช อ-รห สน กศ กษา-คะแนน-เกรด เป นแฟ มใหม รวมท งแสดงช วงคะแนนของแต ละเกรด คะแนน เฉล ย ส วนเบ ยงเบนมาตรฐาน ค าคะแนนส งส ดและต ำส ด ความถ สะสมของแต ละเกรด ลงในแฟ มใหม ด วย Input/Output with files 12